package com.anysoft.utils.login;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.text.TextUtils;

import com.orhanobut.logger.Logger;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * 图片异步下载类
 * 
 */
public abstract class PicDowload extends AsyncTask<String, Void, Bitmap> {

	/** 统计图片的下载完成次数 */
	protected static int count;
	protected static int failCount;
	protected static boolean isRemoveAllViews = true;

	@Override
	protected void onPreExecute() {
		ImageDowloadedBefore();
	}

	@Override
	protected Bitmap doInBackground(String... params) {
		Bitmap bitmap = null;
		String url = params[0];
		if (TextUtils.isEmpty(url)) {
			Logger.i("PicDowload", "要下载的图片URL为空，无法完成下载");
			return null;
		}
		Map<String, String> param = new HashMap<String, String>();
		param.put("abc", "abc");
		param.put("def", "def");
		InputStream in = PictureCodeNetUtil.getInputStreamByUrl(url, param);
		if (in != null)
			bitmap = BitmapFactory.decodeStream(in);
		return bitmap;
	}

	@Override
	protected void onPostExecute(Bitmap result) {
		ImageDowloaded(result);
	}

	/**
	 * 图片已下载完成后要做的事情在此方法写代码
	 * 
	 * @param result
	 *            下载回来的图片
	 */
	protected abstract void ImageDowloaded(Bitmap result);

	/** 图片下载前要做的事情在此方法中写代码 */
	protected void ImageDowloadedBefore() {
	};
}
